//
//  ImageView.m
//  ProjectForA
//
//  Created by 靖哥哥 on 14/11/19.
//  Copyright (c) 2014年 hmengwei. All rights reserved.
//

#import "ImageView.h"
#import "ImageCell.h"
#import "RecommendViewController.h"
#import "ImageDetailsViewController.h"
#import "RecommendModel.h"
@implementation ImageView

- (void)dealloc
{
    [_collection release];
    [_imageArray release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageArray = [[NSMutableArray alloc] initWithCapacity:1];
        
        
        
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
        flowlayout.itemSize = CGSizeMake(self.frame.size.width / 2 - 5, self.frame.size.height / 2 - 5);
        flowlayout.minimumInteritemSpacing = 10;
        self.collection = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowlayout];
        _collection.backgroundColor = [UIColor whiteColor];
        _collection.dataSource = self;
        _collection.delegate = self;
        [_collection registerClass:[ImageCell class] forCellWithReuseIdentifier:@"reuse"];
        [self addSubview:_collection];
        [flowlayout release];
        [_collection release];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    RecommendModel *model = [self.imageArray objectAtIndex:indexPath.item];
    cell.model = model;
    return cell;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageDetailsViewController *imageDetails = [[ImageDetailsViewController alloc] init];
    //响应者链
    UIResponder * responder = self.nextResponder;
    while (responder) {
        if ([responder isKindOfClass:[RecommendViewController class]]) {
            break;
        }
        responder = responder.nextResponder;
    }
    RecommendViewController * vc = (RecommendViewController *)responder;
    [vc.navigationController pushViewController:imageDetails animated:YES];
    NSLog(@"推出 %@", self.nextResponder.nextResponder.nextResponder);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
